﻿#include <QDebug>
#include <QElapsedTimer>
#include <opencv2/opencv.hpp>
#include "test.h"
#include "opencvBuildingBlock/ImageRotate.h"
#include "opencvBuildingBlock/TemplateMatch.h"
#include "opencvBuildingBlock/RotateTemplateMatch.h"
#include "opencvBuildingBlock/PyramidTemplateMatch.h"
#include <qtOpenCV/cvQDebug.h>

cv::RotatedRect buildRotateRect(cv::Mat &model, cv::Rect rect, double angle)
{
    double x = rect.x + rect.width / 2.0;
    double y = rect.y + rect.height / 2.0;
    cv::RotatedRect rrt(cv::Point2f(x, y), model.size(), -angle);

    return rrt;
}


void article01()
{
    cv::Mat src = cv::imread("C:\\Users\\Administrator\\Documents\\templateMatch\\01.bmp");
    cv::imshow("origin picture", src);
    //cv::Mat rot = imageRotateFixSize(src, 30);

    cv::Mat rot, rot_mask;
    imageRotateAutoSize(src, 30, rot, rot_mask);
    cv::imshow("30 deg", rot);
    cv::imshow("mask", rot_mask);
}



void test00()
{
    cv::Mat mask(151, 101, CV_8U, 255);
    cv::Mat rot = imageRotateAutoSize(mask, 0.0);

    qDebug() << "width = " << rot.cols << ", height = " << rot.rows;
}



void speedTest()
{
    QElapsedTimer t;
    cv::Mat src = cv::imread("D:\\templateMatchTestImage\\bird\\bird.jpg", cv::IMREAD_GRAYSCALE);
    cv::Mat model = cv::imread("D:\\templateMatchTestImage\\bird\\model.png", cv::IMREAD_GRAYSCALE);
    cv::namedWindow("origin picture", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
    //cv::imshow("origin picture", src);
    cv::namedWindow("model", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
    cv::imshow("model", model);
    cv::Rect rect;
    double score;

    qDebug() << "Image info: " << src;
    qDebug() << "model info: " << model;
    t.start();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_SQDIFF, rect, score);
    qDebug() << "TM_SQDIFF time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_SQDIFF_NORMED, rect, score);
    qDebug() << "TM_SQDIFF_NORMED time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_CCORR, rect, score);
    qDebug() << "TM_CCORR time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_CCORR_NORMED, rect, score);
    qDebug() << "TM_CCORR_NORMED time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_CCOEFF, rect, score);
    qDebug() << "TM_CCOEFF time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_CCOEFF_NORMED, rect, score);
    qDebug() << "TM_CCOEFF_NORMED time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    cv::rectangle(src, rect, cv::Scalar(0, 0, 0), 2);
    cv::imshow("origin picture", src);
}

void article06()
{
    QElapsedTimer t;
    cv::Mat src = cv::imread("D:\\templateMatchTestImage\\bird\\bird.jpg", cv::IMREAD_GRAYSCALE);
    cv::Mat model = cv::imread("D:\\templateMatchTestImage\\bird\\model.png", cv::IMREAD_GRAYSCALE);
    cv::namedWindow("origin picture", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);

    cv::namedWindow("model", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
    cv::imshow("model", model);

    cv::Rect rect;

    cv::RotatedRect rrt;
    double score;



    Qly::RotateTemplateMatch match;
    //match.setMethod()
    match.setModelByAngleCount(model, -20, 20, 41);

    t.start();
    match.singleMatch(src, 20, rrt, score);
    qDebug() << "TM_SQDIFF_NORMED time elapsed = " << t.elapsed() << "ms, " << rrt << ", score = " << score;
    //cv::rectangle(src, rect, 0, 2);
    Qly::drawRotatedRect(src, rrt, cv::Scalar(0, 0, 0), 2);
    cv::imshow("origin picture", src);
}

void speedTest02()
{
    QElapsedTimer t;
    cv::Mat src = cv::imread("D:\\templateMatchTestImage\\bird\\bird(6.3).jpg", cv::IMREAD_GRAYSCALE);
    cv::Mat model = cv::imread("D:\\templateMatchTestImage\\bird\\model.png", cv::IMREAD_GRAYSCALE);
    cv::namedWindow("origin picture", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);

    qDebug() << src;

    cv::namedWindow("model", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
    cv::imshow("model", model);

    cv::Rect rect;
    cv::RotatedRect rrt;
    double score;
    Qly::RotateTemplateMatch match;
    match.setMethod(cv::TM_CCORR_NORMED);
    match.setModelByAngleCount(model, -20, 20, 41);

    t.start();
    match.rotatedMatch(src, rrt, score);
    qDebug() << "rotatedMatch time elapsed = " << t.elapsed() / 1000.0 << "s, " << rrt << ", score = " << score;
    //cv::rectangle(src, rect, 0, 2);
    Qly::drawRotatedRect(src, rrt, cv::Scalar(0, 0, 0), 2);
    cv::imshow("origin picture", src);
}

void speedTest04()
{
    QElapsedTimer t;
    cv::Mat src = cv::imread("D:\\templateMatchTestImage\\bird\\bird(6.3).jpg", cv::IMREAD_GRAYSCALE);
    cv::Mat model = cv::imread("D:\\templateMatchTestImage\\bird\\model.png", cv::IMREAD_GRAYSCALE);
    cv::namedWindow("origin picture", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);

    qDebug() << src;

    cv::namedWindow("model", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
    cv::imshow("model", model);

    cv::Rect rect;
    cv::RotatedRect rrt;
    double score;
    Qly::PyramidTemplateMatch pymatch(4);
    pymatch.setMethod(cv::TM_CCORR_NORMED);
    pymatch.setModelByAngleCount(model, -32.0, 32.0, (size_t)65);

    t.start();
    pymatch.match(src, rrt, score);
    qDebug() << "rotatedMatch time elapsed = " << t.elapsed() / 1000.0 << "s, " << rrt << ", score = " << score;
    cv::rectangle(src, rect, 0, 2);
    Qly::drawRotatedRect(src, rrt, cv::Scalar(0, 0, 0), 2);
    cv::imshow("origin picture", src);
}

void speedTest03()
{
    QElapsedTimer t;
    cv::Mat src = cv::imread("D:\\templateMatchTestImage\\bird\\bird(6.3).jpg", cv::IMREAD_GRAYSCALE);
    cv::Mat model = cv::imread("D:\\templateMatchTestImage\\bird\\model.png", cv::IMREAD_GRAYSCALE);

//    cv::pyrDown(src, src);
//    cv::pyrDown(model, model);

//    cv::pyrDown(src, src);
//    cv::pyrDown(model, model);

//    cv::pyrDown(src, src);
//    cv::pyrDown(model, model);
    cv::namedWindow("origin picture", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);

    qDebug() << src;

    cv::namedWindow("model", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
    cv::imshow("model", model);

    cv::Rect rect;
    cv::RotatedRect rrt;
    double score;
    Qly::RotateTemplateMatch match;
    match.setMethod(cv::TM_CCORR_NORMED);
    match.setModelByAngleCount(model, -20, 20, 41);

    t.start();
    match.rotatedMatch(src, rrt, score);
    qDebug() << "rotatedMatch time elapsed = " << t.elapsed() / 1000.0 << "s, " << rrt << ", score = " << score;
    //cv::rectangle(src, rect, 0, 2);
    Qly::drawRotatedRect(src, rrt, cv::Scalar(0, 0, 0), 2);
    cv::imshow("origin picture", src);
}

void article05()
{
    QElapsedTimer t;
    cv::Mat src = cv::imread("D:\\templateMatchTestImage\\bird\\bird.jpg", cv::IMREAD_GRAYSCALE);
    cv::Mat model = cv::imread("D:\\templateMatchTestImage\\bird\\model.png", cv::IMREAD_GRAYSCALE);
    cv::namedWindow("origin picture", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
    cv::imshow("origin picture", src);
    cv::namedWindow("model", cv::WINDOW_NORMAL | cv::WINDOW_KEEPRATIO);
    cv::imshow("model", model);
    cv::Rect rect;
    double score;

    t.start();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_SQDIFF, rect, score);
    qDebug() << "TM_SQDIFF time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_SQDIFF_NORMED, rect, score);
    qDebug() << "TM_SQDIFF_NORMED time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_CCORR, rect, score);

//    cv::Mat result;
//    cv::matchTemplate( src, model, result, cv::TM_CCORR );
//    double minVal, maxVal;
//    cv::Point minLoc, maxLoc;
//    cv::minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc);
    qDebug() << "TM_CCORR time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;
//    qDebug() << "x = " << maxLoc.x;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_CCORR_NORMED, rect, score);
    qDebug() << "TM_CCORR_NORMED time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_CCOEFF, rect, score);
    qDebug() << "TM_CCOEFF time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;

    t.restart();
    Qly::templateMatch(src, model, cv::Mat(), cv::TM_CCOEFF_NORMED, rect, score);
    qDebug() << "TM_CCOEFF_NORMED time elapsed = " << t.elapsed() << "ms, " << rect << ", score = " << score;


    cv::rectangle(src, rect, cv::Scalar(0, 0, 0), 2);
    cv::imshow("origin picture", src);
    //cv::Mat rot = imageRotateFixSize(src, 30);

//    cv::Mat rot, rot_mask;
//    imageRotateAutoSize(src, 30, rot, rot_mask);
//    cv::imshow("30 deg", rot);
//    cv::imshow("mask", rot_mask);
}

